package svc

import (
	"context"
	"gozeroSingle/internal/config"
	"gozeroSingle/internal/model"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type ServiceContext struct {
	Config config.Config

	// 注入模型
	UserModel                 model.MallUserModel
	BannerModel               model.MallBannerModel
	OrderModel                model.MallOrderModel
	OrderItemModel            model.MallOrderItemModel
	OrderDeliveryAddressModel model.MallOrderDeliveryAddressModel
	OrderExpressModel         model.MallOrderExpressModel
	AccountModel              model.MallAccountModel
	AccountRoleModel          model.MallAccountRoleModel
	RoleModel                 model.MallRoleModel
	RoleAuthModel             model.MallRoleAuthModel
	AuthModel                 model.MallAuthModel
	Logger                    logx.Logger
}

func NewServiceContext(c config.Config) *ServiceContext {
	// ------------- 获取mysql连接开始 -------------
	datasource := ""
	switch c.Env {
	case "dev":
		datasource = c.MysqlDB.Dev.DataSource
	case "test":
		datasource = c.MysqlDB.Test.DataSource
	case "prod":
		datasource = c.MysqlDB.Prod.DataSource
	}
	sc := sqlx.NewMysql(datasource)
	// ------------- 获取mysql连接结束 -------------

	return &ServiceContext{
		Config: c,

		// 创建模型
		UserModel:                 model.NewMallUserModel(sc),
		BannerModel:               model.NewMallBannerModel(sc),
		OrderModel:                model.NewMallOrderModel(sc),
		OrderItemModel:            model.NewMallOrderItemModel(sc),
		OrderDeliveryAddressModel: model.NewMallOrderDeliveryAddressModel(sc),
		OrderExpressModel:         model.NewMallOrderExpressModel(sc),
		AccountModel:              model.NewMallAccountModel(sc),
		AccountRoleModel:          model.NewMallAccountRoleModel(sc),
		RoleModel:                 model.NewMallRoleModel(sc),
		RoleAuthModel:             model.NewMallRoleAuthModel(sc),
		AuthModel:                 model.NewMallAuthModel(sc),
		Logger:                    logx.WithContext(context.Background()),
	}
}
